package com.brook.springcloud.controller;

import com.brook.springcloud.pojo.Dept;
import com.brook.springcloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Brook
 */
@RestController
public class DeptController {

    @Resource
    private DeptService deptService;

    /**
     * 获取一些配置信息，得到具体的微服务
     */
    @Resource
    private DiscoveryClient client;

    @PostMapping("/dept/add")
    private boolean addDept(@RequestBody Dept dept) {
        return deptService.addDept(dept);
    }

    @GetMapping("/dept/get/{id}")
    private Dept get(@PathVariable("id") Long id) {
        return deptService.queryId(id);
    }


    @RequestMapping("/dept/list")
    private List<Dept> getAll() {
        return deptService.queryAll();
    }

    //注册进来的微服务，获取一些消息
    @GetMapping("/dept/discovery")
    public Object discovery() {
        //获取微服务列表清单
        List<String> services = client.getServices();
        System.out.println("discovery=>services:"+services);
        //得到1一个具体的微服务信息,通过具体的微服务名称，就是applciaton.name
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance serviceInstance: instances){
            System.out.println(
                    serviceInstance.getHost()+"\t"+
                            serviceInstance.getPort()+"\t"+
                            serviceInstance.getUri()+"\t"+
                            serviceInstance.getServiceId()
            );
        }

        return this.client;
    }
}
